#version 330 core
out vec4 FragColor;

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

uniform vec3 viewPos;
uniform samplerCube skybox;

// 材质
struct Material {
    sampler2D diffuse;
    sampler2D specular;
    sampler2D reflect;
    sampler2D reflact;
};

// 定向光
struct DirLight {
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

// 点光源
struct PointLight {
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;
};

// 聚光
struct SpotLight {
    vec3 position;
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;

    float cutOff;
    float outerCutOff;
};

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

// 控制点光源和聚光的数量
#define NR_POINT_LIGHT 4
#define NR_SPOT_LIGHT 4

uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHT];
uniform SpotLight spotLights[NR_SPOT_LIGHT];


void main()
{
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);
    
    // 定向光照
    vec3 result = CalcDirLight(dirLight, norm, viewDir);

    // 叠加点光源，根据常数项判定有没有光源
    for (int i = 0; i < NR_POINT_LIGHT; i++)
        if (pointLights[i].constant != 0)
            result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
       
    // 叠加聚光，根据常数项判定有没有光源
    for (int i = 0; i < NR_SPOT_LIGHT; i++)
        if (spotLights[i].constant != 0)
            result += CalcSpotLight(spotLights[i], norm, FragPos, viewDir);

    // 计算反射
    vec3 I = normalize(FragPos - viewPos);
    vec3 R = reflect(I, norm);
    result += texture(skybox, R).rgb * vec3(texture(material.reflect, TexCoords));

    // 计算折射
    float ratio = 1.00 / 1.52;
    R = refract(I, norm, ratio);
    result += texture(skybox, R).rgb * vec3(texture(material.reflact, TexCoords));

    vec4 texColor = texture(material.diffuse, TexCoords);
    FragColor = vec4(result, texColor.a);
}

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
    vec3 lightDir = normalize(-light.direction);

    // 环境光反射
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

    // 镜面反射
    vec3 reflectDir = reflect(-lightDir, normal);

    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

    // 合并结果
    return (ambient + diffuse + specular);
}

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // 计算距离衰减
    float dis = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * dis + light.quadratic * (dis * dis));

    // 环境光反射
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

    // 镜面反射
    vec3 reflectDir = reflect(-lightDir, normal);
    
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

    // 合并结果
    return (ambient + diffuse + specular) * attenuation;
}

vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // 计算距离衰减
    float dis = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * dis + light.quadratic * (dis * dis));

    // 环境光反射
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

    // 镜面反射
    vec3 reflectDir = reflect(-lightDir, normal);

    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

    // 计算夹角余弦，比较传入的余弦
    float theta = dot(lightDir, normalize(-light.direction));
    float epsilon = light.cutOff - light.outerCutOff;

    // clamp 函数约束取值范围
    float insensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);

    // 合并结果
    return (ambient + diffuse * insensity + specular * insensity) * attenuation;
}